package com.c.rabbit_mq.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.ToolOpt;
import com.c.rabbit_mq.ComRabbitMqHandle;
import com.tool.ToolMqtt;
import com.tool.rabbit.ToolRabbitMQ;
import ntd.protocol.Ntd212Parse;
import ntd.protocol.bo.Ntd212BO;
import ntd.protocol.context.Context212;
import ntd.util.UtilThreadPool;
import ntd.util.frame.task.handle.fm.util.ToolFmData;
import ntd.util.modular.data.util.handle.constant.Constant212;
import ntd.util.modular.data.util.handle.tool.Tool212;
import ntd.util.modular.things.entity.Device;

import static com.LJ212Listen.handle212Data;
import static com.LJ212Listen.handle9802;

/**
 * mqtt 消息统一处理
 */
public class Iot212MsgExe extends ComRabbitMqHandle<String> {

    public static final String TOPIC = "/com-server/212/msg";

    public Iot212MsgExe() {
        super(TOPIC, String.class);
    }

    @Override
    public void handle(String topic, String msg) {
        if (StrUtil.isBlank(msg)) {
            return;
        }

        try {
            Context212 context212 = Ntd212Parse.parse(msg);
            Ntd212BO ntd212BO = context212.getNtd212BO();

            if (StrUtil.isBlank(ntd212BO.getMN())) {
                StaticLog.error("LJ报文缺少mn编号[{}]", msg);
                return;
            }

            Device device = ToolOpt.OPT_T_DEVICE.getByCode(ntd212BO.getMN());

            // 富铭，设备在线推送
            UtilThreadPool.run(() -> ToolFmData.OPT_FM_ON_LINE.onLine(ntd212BO.getMN()));

            // 数据处理(212)
            if (Tool212.DATA_CNS.contains(ntd212BO.getCN())) {
                StaticLog.info("LJMQTT收到数据开始处理数据-数据采集-[{}]", msg);
                UtilThreadPool.run(() -> handle212Data(ntd212BO));
            } else if (ntd212BO.getCN().equals(StrUtil.toString(Constant212.CN_9802))) {
                StaticLog.info("LJMQTT收到数据开始处理数据-智能排污-[{}]", msg);
                handle9802(ntd212BO, device);
            } else {
                StaticLog.error("212 非上报数据包内容（记录到日志） - " + msg);
            }
        } catch (Exception e) {
            StaticLog.error(e, "[212][处理端][{}]失败...", msg);
        }
    }

    public static void push(String msg) {
        ToolRabbitMQ.push(Iot212MsgExe.TOPIC, msg);
    }

    public static void pushMqtt(String mn, String msg) {
        String topic = StrUtil.format("{}/{}", TOPIC, mn);
        ToolMqtt.mqtt.pushQos2(topic, msg);
    }

    public static void main(String[] args) {
        new Iot212MsgExe().handle("", "##0363ST=32;CN=2061;QN=20231214000052976;PW=123456;MN=41060201G00009;Flag=1;CP=&&DataTime=20231213230000;B01-Min=2.24,B01-Avg=2.25,B01-Max=2.27,B01-Cou=8.11;011-Min=95.69,011-Avg=95.69,011-Max=97.74,011-Cou=0.7761;027-Min=0.000,027-Avg=0.000,027-Max=0.000,027-Cou=0;001-Min=8.255,001-Avg=8.272,001-Max=8.273;060-Min=10##0247ST=32;CN=2011;QN=20231214000057220;PW=123456;MN=41060201G00009;Flag=0;CP=&&DataTime=20231214000057;B01-Rtd=2.31,B01-Flag=N;BO2-Cou=_,BO2-Flag=N;011-Rtd=95.69,011-Flag=N;027-Rtd=0.000000,027-Flag=N;001-Rtd=8.28,001-Flag=N;060-Rtd=10.89,060-Flag=N&&7EC0\n");
    }
}
